<?php

namespace App\Service\Index\v1_2_2;

use App\Constants\ErrorCode;
use App\Model\CallBase;
use App\Model\InviteVideoLog;
use App\Model\UserCoin;
use App\Model\VideoCall;
use App\Model\VideoCallAuditConfig;
use App\Service\BaseService;

/**
 * 接听通话
 */
class AnswerCallService extends BaseService
{
    protected function RoVerifyList()
    {
        return [
            'caller_user_id' => 'required|int',
            'call_type' => 'required|int',
        ];
    }

    public function handle()
    {
        $fromSys = $this->request->input('from_sys', 0);
        //系统呼叫
        if ($fromSys == 1) return $this->setError(ErrorCode::COIN_NOT_ENOUGH, '剩余金币不足');

        $chatUserId = $this->request->input('caller_user_id', 0);
        $callType = $this->request->input('call_type', VideoCall::CALL_TYPE);
        $callCls = CallBase::getCallClass($callType);
        $callRecord = $callCls::checkRecord($this->userId, $chatUserId);
        //无记录
        if (!$callRecord) {
            return $this->setError(ErrorCode::PARAMETER_ERROR, '该话单不存在');
        }
        //话单状态
        // if ($callRecord->status != CallBase::STATUS_NO_PICK) {
        //     return $this->setError(ErrorCode::PARAMETER_ERROR, '该话单状态异常，暂无法接听');
        // }

        //主播呼叫-常规
        if (CallBase::FROM_HOST_CALL == $callRecord->from) {
            $userCoin = UserCoin::getUserInfo($this->userId);
            if (!$userCoin || $userCoin['total_coin'] < $callRecord->coin_setting) {
                return $this->setError(ErrorCode::COIN_NOT_ENOUGH, '剩余金币不足');
            }
        }

        if (CallBase::FROM_INVITE_CALL == $callRecord->from)
        {
            InviteVideoLog::updateStatus($callRecord->chat_user_id,$callRecord->user_id,InviteVideoLog::STATUS_ANSWERING);
        }

        $config = (new VideoCallAuditConfig())->getConfig($this->userId);

        return ['result' => 1, 'jHConfig' => $config];
    }
}
